package com.auction.controller.user;

import com.auction.dto.AuctionDTO;
import com.auction.dto.AuctionPageQueryDTO;
import com.auction.dto.SellerApplicationDTO;
import com.auction.pojo.*;
import com.auction.service.AuctionService;
import com.auction.service.SellerService;
import com.auction.utils.BaseContext;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController("userSellerController")
@RequestMapping("/user/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @Autowired
    private AuctionService auctionService;

    /**
     * 提交卖家申请
     * 
     * @param sellerApplicationDTO 卖家申请信息
     * @return 申请结果
     */
    @PostMapping("/apply")
    public Result<String> apply(@RequestBody SellerApplicationDTO sellerApplicationDTO) {
        log.info("提交卖家申请：{}", sellerApplicationDTO);
        Long userId = BaseContext.getCurrentId();
        sellerApplicationDTO.setUserId(userId);
        sellerService.apply(sellerApplicationDTO);
        return Result.success("申请提交成功，请等待审核");
    }

    /**
     * 查询申请状态
     * 
     * @return 申请状态
     */
    @GetMapping("/application/status")
    public Result<SellerApplication> getApplicationStatus() {
        Long userId = BaseContext.getCurrentId();
        log.info("查询卖家申请状态：userId={}", userId);
        SellerApplication application = sellerService.getByUserId(userId);
        return Result.success(application);
    }

    /**
     * 获取卖家信息
     * 
     * @return 卖家信息
     */
    @GetMapping("/info")
    public Result<SellerInfo> getSellerInfo() {
        Long userId = BaseContext.getCurrentId();
        log.info("获取卖家信息：userId={}", userId);
        SellerInfo sellerInfo = sellerService.getSellerInfoByUserId(userId);
        return Result.success(sellerInfo);
    }

    /**
     * 发布拍卖品
     * 
     * @param auctionDTO 拍卖品信息
     * @return 拍卖品ID
     */
    @PostMapping("/auction")
    public Result publishAuction(@RequestBody AuctionDTO auctionDTO) {
        Long userId = BaseContext.getCurrentId();
        Long sellerId = sellerService.getSellerInfoByUserId(userId).getId();
        log.info("卖家{}发布拍卖品：{}", sellerId, auctionDTO);
        auctionService.publish(auctionDTO, sellerId);
        return Result.success();
    }

    /**
     * 根据id查询拍卖品详情
     *
     * @param id 拍卖品ID
     * @return 拍卖品详情
     */
    @GetMapping("/auction/{id}")
    public Result<Auction> getById(@PathVariable Long id) {
        log.info("获取拍卖品详情：id={}", id);
        Auction auction = auctionService.getById(id);
        return Result.success(auction);
    }

    /**
     * 更新拍卖品信息
     * 
     * @param auctionDTO 拍卖品信息
     * @return 更新结果
     */
    @PutMapping("/auction")
    public Result updateAuction(@RequestBody AuctionDTO auctionDTO) {
        Long userId = BaseContext.getCurrentId();
        Long sellerId = sellerService.getSellerInfoByUserId(userId).getId();
        log.info("卖家{}更新拍卖品：{}", sellerId, auctionDTO);
        auctionService.update(auctionDTO, sellerId);
        return Result.success();
    }

    /**
     * 分页查询卖家发布的拍卖品列表
     *
     * @param auctionPageQueryDTO 查询条件
     * @return 拍卖品列表
     */
    @GetMapping("/auction/page")
    public Result<PageResult> pageSellerAuctions(AuctionPageQueryDTO auctionPageQueryDTO) {
        Long userId = BaseContext.getCurrentId();
        Long sellerId = sellerService.getSellerInfoByUserId(userId).getId();
        log.info("查询卖家拍卖品列表：sellerId={}", sellerId);
        auctionPageQueryDTO.setSellerId(sellerId);
        PageResult pageResult = auctionService.page(auctionPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 取消拍卖
     * 
     * @param id 拍卖品ID
     * @return 取消结果
     */
    @PutMapping("/auction/cancel/{id}")
    public Result cancelAuction(@PathVariable Long id) {
        Long userId = BaseContext.getCurrentId();
        Long sellerId = sellerService.getSellerInfoByUserId(userId).getId();
        log.info("卖家{}取消拍卖品：id={}", sellerId, id);
        auctionService.cancel(id, sellerId);
        return Result.success();
    }

    /**
     * 删除拍卖品
     *
     * @param id 拍卖品ID
     * @return 删除结果
     */
    @DeleteMapping("/auction/{id}")
    public Result deleteAuction(@PathVariable Long id) {
        Long userId = BaseContext.getCurrentId();
        Long sellerId = sellerService.getSellerInfoByUserId(userId).getId();
        log.info("卖家{}删除拍卖品：id={}", sellerId, id);
        auctionService.delete(id, sellerId);
        return Result.success();
    }
}